﻿using DevExpress.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using VideoLib.Utils;
using VideoLib.ViewModels;
using DataFormats = System.Windows.DataFormats;
using DataObject = System.Windows.DataObject;
using DragDropEffects = System.Windows.DragDropEffects;

namespace VideoLib.Models
{
    public class FolderInfo
    {
        public string? Direction { get; set; }
        public ObservableCollection<VideoInfo> Videos { get; set; } = new ObservableCollection<VideoInfo>();
    }

    public class VideoInfo : BindableBase
    {
        public VideoInfo()
        {
        }

        private int thumbNum = 12;
        public void MouseLeave()
        {
            isMouseOver = false;
            DelTmp();

            PreviewImg = null;
            MainViewModel.ShowDynamicImage(PreviewImg);
        }

        private void DelTmp()
        {
            for (int x = 0; x < thumbNum; x++)
            {
                if (File.Exists($".\\temp\\{Name}_{x}.png"))
                {
                    File.Delete($".\\temp\\{Name}_{x}.png");
                }
            }
        }

        bool isMouseOver = false;
        public async void MouseEnter()
        {
            isMouseOver = true;
            await Task.Delay(500);
            if (!isMouseOver || Path == null)
            {
                return;
            }

            int x = 0;
            TimeSpan duration = await FFmpegHelper.GetVideoDuration(Path);
            double gap = (duration.TotalMilliseconds - 500) / thumbNum;
            TimeSpan timeSpan = TimeSpan.FromMilliseconds(gap);

            for (int i = 0; i < thumbNum; i++)
            {
                if (!isMouseOver)
                {
                    return;
                }

                await FFmpegHelper.GenerateVideoPreview(Path, $".\\temp\\{Name}_{x}.png", timeSpan.ToString(@"hh\:mm\:ss"));
                timeSpan = timeSpan + TimeSpan.FromMilliseconds(gap);

                if (!isMouseOver)
                {
                    return;
                }

                try
                {
                    var uri = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, $"temp\\{Name}_{x}.png"), UriKind.Relative);

                    BitmapImage bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = uri;
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();

                    PreviewImg = bitmap;
                    MainViewModel.ShowDynamicImage(PreviewImg);
                    x++;
                    await Task.Delay(50);
                }
                catch (Exception)
                {
                    x++;
                }
            }

            DelTmp();
            MouseEnter();
        }

        public string? Name { get; set; }

        public string? Path { get; set; }

        public BitmapImage? Thumb { get; set; }

        public BitmapImage? PreviewImg { get => GetValue<BitmapImage>(); set => SetValue(value); }
        public void MouseMove(DependencyObject dependency)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed)
            {
                DataObject dragData = new DataObject(DataFormats.FileDrop, new string[] { Path });
                DragDrop.DoDragDrop(dependency, dragData, DragDropEffects.Copy);
            }
        }
    }
}
